package com.hy.controller;

import com.hy.bean.ReceiveApply;
import com.hy.bean.ResponseResult;
import com.hy.service.ReceiveApplyService;
import com.hy.service.impl.ReceiveApplyServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("receive")
public class ReceiveApplyController {
    private ReceiveApplyService receiveApplyService = new ReceiveApplyServiceImpl();
    @GetMapping("queryList/{deptName}/{username}")
    public ResponseResult queryList(@PathVariable String deptName,@PathVariable String username) throws Exception {
        List<Map<String, Object>> mapList = receiveApplyService.select(deptName, username);
        if (mapList==null) {
            return new ResponseResult(200,"没数据",10002,null);
        }
        return new ResponseResult(200,"查询完成",10000,mapList);
    }

    @PostMapping("add")
    public ResponseResult add(@RequestBody ReceiveApply receiveApply, HttpServletRequest request) throws Exception {
        HttpSession session = request.getSession();
        Object id = session.getAttribute("id");
        receiveApply.setDjr(Long.parseLong(id.toString()));
        Map<String, Object> newest = receiveApplyService.insert(receiveApply);
        if (newest !=null)
            return new ResponseResult(200,"插入成功",10001,newest);
        else
            return new ResponseResult(250,"插入失败",10000,null);
    }

    @DeleteMapping("removeApply/{id}")
    public ResponseResult removeApply(@PathVariable Integer id) throws Exception {
        Boolean flag = receiveApplyService.deleteApply(id);

        if (flag)
            return new ResponseResult(200,"删除成功",10001,flag);
        else
            return new ResponseResult(250,"删除失败",10000,false);
    }

    @PutMapping("modify")
    public ResponseResult modify(@RequestBody ReceiveApply receiveApply,HttpServletRequest request) throws Exception {
        HttpSession session = request.getSession();
        Object id = session.getAttribute("id");
        receiveApply.setDjr(Long.parseLong(id.toString()));
        Boolean flag = receiveApplyService.update(receiveApply);
        return new ResponseResult<>(200,"编辑成功",1000,flag);
    }

    @PutMapping("modifyDjstatus/{id}")
    public ResponseResult modifyDjstatus(@PathVariable Integer id) throws Exception {
        return new ResponseResult<>(200,"编辑成功",1000,receiveApplyService.updateDjstatus(id));
    }
}
